//
//  NSUserDefaultContentSaveViewController.m
//  数据存储
//
//  Created by whz on 17/2/27.
//  Copyright © 2017年 whz. All rights reserved.
//

#import "NSUserDefaultContentSaveViewController.h"
#import "ViewController.h"

@interface NSUserDefaultContentSaveViewController ()

@property(nonatomic, weak)NSUserDefaults *userDefault;
@property(nonatomic, strong)UITextField *textField;
@property(nonatomic, strong)UILabel *label;

@end

@implementation NSUserDefaultContentSaveViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    
    [self backBtn];
    
    [self saveContent];
}

- (void)saveContent {

    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    self.userDefault = userDefault;
    
    NSLog(@"%@",NSHomeDirectory());
    
    [userDefault removeObjectForKey:@"textFieldContent"];
    
    if ([userDefault objectForKey:@"textFieldContent"]) {
        NSLog(@"存进来了");
    } else {
        NSLog(@"没存进来");
    }
    
    [userDefault setObject:self.textField.text forKey:@"textFieldContent"];
    
    if ([userDefault objectForKey:@"textFieldContent"]) {
        NSLog(@"存进来了");
    } else {
        NSLog(@"没存进来");
    }
    [UIView animateWithDuration:2.0 animations:^{
        self.label.text = [userDefault objectForKey:@"textFieldContent"];
    }];
}

- (void)setupUI {
    
    [self.label setText:@"000"];
    self.label.textAlignment = NSTextAlignmentCenter;
    
    self.textField.text = @"111";
    self.textField.textAlignment = NSTextAlignmentCenter;
}

- (void)backBtn {
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-100, self.view.frame.size.height-50, 100, 50)];
    [self.view addSubview:btn];
    [btn setTitle:@"back" forState:UIControlStateNormal];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    [btn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backVcBtn) forControlEvents:UIControlEventTouchUpInside];
}

- (void)backVcBtn {
    
    ViewController *vc = [[ViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

- (UITextField *)textField {
    
    if (!_textField) {
        
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(self.view.center.x-50, 100, 100, 50)];
        [self.view addSubview:_textField];
    }
    return _textField;
}

- (UILabel *)label {
    
    if (!_label) {
        
        _label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.center.x-50, 150, 100, 50)];
        [self.view addSubview:_label];
    }
    return _label;
}

@end
